package dao

import models.{User, Message}

//implementación
import com.db4o.ObjectContainer
import com.db4o.query.Query

object MessageDAO extends CommonDAO[Message] {
  
  def createAndRef(author:User, destination:User, title:String, content:String)
                   (implicit s:Session): Option[Message] = {
    val m:Message = new Message(author, destination, title, content, new java.util.Date)
    if(save(m)){
      Some(m)
    } else {
      None
    }
  }
  
  def findByID(id:Long)(implicit s:Session) :Option[Message] = {
    try{
      val m:Message = s.get.ext().getByID(id)
      if(m != null){
        s.get.activate(m, 4) //XXX: Explicit activation
        Some(m)
      } else {
        None
      }
    } catch {
      case ex:Exception => None
    }
  }
  
  def getID(m:Message)(implicit s:Session):Long = {
    s.get.ext().getID(m)
  }
}